package com.meshnetics.gb.stack.mac.primitives;

import com.meshnetics.gb.stack.core.event.StackConfirm;
import com.meshnetics.gb.stack.core.event.StackEvent;
import com.meshnetics.gb.stack.core.event.StackEventType;
import com.meshnetics.gb.stack.core.layer.StackLayer;
import com.meshnetics.gb.stack.mac.RadioMACStackLayer;
import com.meshnetics.gb.stack.mac.type.MACStatus;
import com.meshnetics.gb.stack.mac.type.TypeMACStatus;
import com.meshnetics.gb.type.impl.ByteUtils;
import com.meshnetics.gb.type.impl.zcl.TypeUInt;
import com.meshnetics.gb.type.impl.zcl.UInt;

import java.nio.ByteBuffer;

/**
 * WRITE DESCRIPTION HERE
 *
 * @author AKovylin
 */
public class MACSetConfirm extends StackConfirm implements MACReplyOnCommand {
    public static final byte CODE = 0x5c;

    private static final byte[] ALLOWED_STATUSES = new byte[]{
            MACStatus.SUCCESS.getCode(),
            MACStatus.UNSUPPORTED_ATTRIBUTE.getCode(),
            MACStatus.INVALID_PARAMETER.getCode()
    };

    private final UInt pibAttribute;

    public MACSetConfirm(ByteBuffer bb, RadioMACStackLayer layer) {
        super("macSetConfirm", StackEventType.LME_TYPE, null, null, layer);
        ByteUtils.ensureCapacity(bb, 2);
        MACStatus status = (MACStatus) TypeMACStatus.TYPE.deserialize(bb);
        Utils.checkStatus(status, ALLOWED_STATUSES);
        setStatus(status);
        this.pibAttribute = (UInt) TypeUInt.UINT_8.deserialize(bb);
    }

    public MACSetConfirm(MACStatus status, int pibAttribute) {
        this(status, (byte) (pibAttribute & 0xff));
    }

    public MACSetConfirm(MACStatus status, byte pibAttribute) {
        this(status, new Byte(pibAttribute));
    }

    public MACSetConfirm(MACStatus status, Byte pibAttribute) {
        this(status, (UInt) TypeUInt.UINT_8.fromObject(pibAttribute));
    }

    public MACSetConfirm(MACStatus status, UInt pibAttribute) {
        this(status, pibAttribute, null);
    }

    public MACSetConfirm(MACStatus status, UInt pibAttribute, StackLayer source) {
        super("macSetConfirm", StackEventType.LME_TYPE, null, null, source);
        if (status == null) {
            throw new NullPointerException("status is null");
        }
        Utils.checkStatus(status, ALLOWED_STATUSES);
        if (pibAttribute == null) {
            throw new NullPointerException("pibAttribute is null");
        }
        setStatus(status);
        this.pibAttribute = pibAttribute.convert(TypeUInt.UINT_8);
    }

    public UInt getPibAttributeType() {
        return pibAttribute;
    }

    public Byte getPibAttribute() {
        return (Byte) pibAttribute.toObject();
    }

    public boolean isReplyOn(StackEvent se) {
        return se instanceof MACSetRequest && pibAttribute.equals(((MACSetRequest) se).getPibAttributeType());
    }

    public String toString() {
        return getName() + '(' + getStatus() + ',' + pibAttribute + ')';
    }
}
